package com.burgeon.backend.Util.wechatUtil;



import com.burgeon.backend.Util.menu.WeChatInfo;
import com.burgeon.backend.Util.menu.WeixinUtil;
import com.burgeon.backend.WeChat.GetAccesToken;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class WeXinHuiFu {


    private static final String REQ_MESSAGE_TYPE_IMAGE = "image";



    public static void sendTextMessageToAd(String content, String toUser){
        String json = "{\"touser\": \""+toUser+"\",\"msgtype\": \"text\", \"text\": {\"content\": \""+content+"\"}}";

        //获取请求路径
        String action = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=";

        try {
            connectWeiXinInterface(action,json);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 微信公共账号发送给账号
     * @param content 文本内容
     * @param toUser 微信用户
     * @return
     */
    public static void sendTextMessageToUser(String content, String toUser,String accessToken){
        System.err.println(toUser);;
        String json = "{\"touser\": \""+toUser+"\",\"msgtype\": \"text\", \"text\": {\"content\": \""+content+"\"}}";
        //获取access_token
//        GetExistAccessToken getExistAccessToken = GetExistAccessToken.getInstance();
//        String accessToken = getExistAccessToken.getExistAccessToken();
      //  String accessToken = WeGetAccessToken.getAccessToken();
      //  String accessToken = WeixinUtil.getAccessToken(WeChatInfo.WX_APPID,WeChatInfo.WX_APPSECRET).getToken();
//        String access_token = getAccesToken.GetAccesToken(WeChatInfo.token);
        //获取请求路径
        String action = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+accessToken;

        try {
           // connectWeiXinInterface(action,json);
            WeixinUtil.httpRequest(action,"POST",json);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 微信公共账号发送给账号(本方法限制使用的消息类型是语音或者图片)
     * @param mediaId 图片或者语音内容
     * @param toUser 微信用户
     * @return
     */
    public static void sendPicOrVoiceMessageToUser(String mediaId,String toUser,String accessToken) {

            String json = "{\"touser\": \"" + toUser + "\",\"msgtype\": \"image\", \"image\": {\"media_id\": \"" + mediaId + "\"}}";
//        }else if(msgType.equals(REQ_MESSAGE_TYPE_VOICE)){
//            json = "{\"touser\": \""+toUser+"\",\"msgtype\": \"voice\", \"voice\": {\"media_id\": \""+mediaId+"\"}}";
//        }
            //获取请求路径
            String action = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accessToken;
            try {
//                connectWeiXinInterface(action, json);
                WeixinUtil.httpRequest(action,"POST",json);

            } catch (Exception e) {
                e.printStackTrace();
            }

    }


    /**
     *  发送图文给所有的用户
     * @param openId 用户的id
     */
//    public  void sendNewsToUser(String openId){
//        MediaUtil mediaUtil = MediaUtil.getInstance();
//        ArrayList<Object> articles = new ArrayList<Object>();
//        Article a = new Article();
//        articles.add(a);
//        String str = JsonUtil.getJsonStrFromList(articles);
//        String json = "{\"touser\":\""+openId+"\",\"msgtype\":\"news\",\"news\":" +
//                "{\"articles\":" +str +"}"+"}";
//        json = json.replace("picUrl", "picurl");
//        System.out.println(json);
//        //获取access_token
//        String access_token = mediaUtil.getAccess_token();
//        //获取请求路径
//        String action = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+access_token;
//        try {
//            connectWeiXinInterface(action,json);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }








    /**
     * 连接请求微信后台接口
     * @param action 接口url
     * @param json  请求接口传送的json字符串
     */
    public static void connectWeiXinInterface(String action, String json){
        URL url;
        try {

            url = new URL(action);
            HttpURLConnection http = (HttpURLConnection) url.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            http.setDoOutput(true);
            http.setDoInput(true);
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
            http.connect();
            OutputStream os = http.getOutputStream();
            os.write(json.getBytes("UTF-8"));// 传入参数
            InputStream is = http.getInputStream();
            int size = is.available();
            byte[] jsonBytes = new byte[size];
            is.read(jsonBytes);
            String result = new String(jsonBytes, "UTF-8");
            System.out.println("请求返回结果:"+result);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
